package com.software.levelupandroid.taskIssue.model;

import android.content.Context;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.software.levelupandroid.entity.Result;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.taskIssue.CallBack;
import com.software.levelupandroid.utils.OkhttpUtil;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Description 任务新增的Model实例
 * @Author LiangYaJie
 * @CreateTime 2024年05月15日 11:14:53
 */
public class TaskIssueModel implements IModel {

    /**
     * 新增任务
     * @param task
     * @param context
     * @param callBack
     */
    @Override
    public void issueTask(final Task task, Context context, final CallBack callBack) {
        String taskJson = new Gson().toJson(task);

        RequestBody requestBody = RequestBody.create(
                MediaType.parse("application/json;charset=UTF-8"),
                taskJson
        );

        //发送异步请求
        Call call = OkhttpUtil.postRequest(requestBody, "task/insertTask", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String respJson = response.body().string();
                Gson gson = new Gson();

                Result<String> result = gson.fromJson(respJson, Result.class);
                if (result != null && result.getCode().equals("200")){
                    callBack.onSuccess(task);
                }else {
                    callBack.onFailure("新增失败");
                }
            }
        });

    }

    /**
     * 更新任务
     * @param task
     * @param context
     * @param callBack
     */
    @Override
    public void updateTask(Task task, Context context, CallBack callBack) {
        String taskJson = new Gson().toJson(task);

        RequestBody requestBody = RequestBody.create(
                MediaType.parse("application/json;charset=UTF-8"),
                taskJson
        );

        //发送异步请求
        Call call = OkhttpUtil.postRequest(requestBody, "task/updateTask", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String respJson = response.body().string();
                Gson gson = new Gson();

                Result<String> result = gson.fromJson(respJson, Result.class);
                if (result != null && result.getCode().equals("200")){
                    callBack.onSuccess(task);
                }else {
                    callBack.onFailure("更新失败");
                }
            }
        });
    }

    /**
     * 任务是否完成
     * @param task
     * @param context
     */
    public void isFinishTask(Task task, CallBack callBack, Context context){
        String taskJson = new Gson().toJson(task);

        RequestBody requestBody = RequestBody.create(
                MediaType.parse("application/json;charset=UTF-8"),
                taskJson
        );

        //发送异步请求
        Call call = OkhttpUtil.postRequest(requestBody, "task/updateTask", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String respJson = response.body().string();
                Gson gson = new Gson();

                Result<String> result = gson.fromJson(respJson, Result.class);
                if (result != null && result.getCode().equals("200")){
                    callBack.onSuccess(task);
                }else {
                    callBack.onFailure("状态更新失败");
                }
            }
        });
    }

    /**
     * 任务删除
     * @param task
     * @param context
     * @param callBack
     */
    public void deleteTask(Task task, Context context, CallBack callBack){
        String taskJson = new Gson().toJson(task);

        RequestBody requestBody = RequestBody.create(
                MediaType.parse("application/json;charset=UTF-8"),
                taskJson
        );

        //发送异步请求
        Call call = OkhttpUtil.postRequest(requestBody, "task/deleteTask", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String respJson = response.body().string();
                Gson gson = new Gson();

                Result<String> result = gson.fromJson(respJson, Result.class);
                if (result != null && result.getCode().equals("200")){
                    callBack.onSuccess(task);
                }else {
                    callBack.onFailure("删除失败");
                }
            }
        });
    }

    /**
     * 更新用户经验值
     * @param experience
     * @param context
     * @param callBack
     */
    public void updateExperience(double experience, Context context, com.software.levelupandroid.home.CallBack callBack){
        RequestBody requestBody = new FormBody.Builder()
                .add("experience", String.valueOf(experience))
                .build();

        Call call = OkhttpUtil.postRequest(requestBody, "user/updateExperience", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String respJson = response.body().string();
                Gson gson = new Gson();

                Result<String> result = gson.fromJson(respJson, Result.class);
                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        String data = result.getData();
                        callBack.onSuccess(data);
                    }else {
                        callBack.onFailure("result.getCode==null||result.getCode.isNotEquals('200'): 获取任务失败");
                    }
                }else {
                    callBack.onFailure("result==null: 未成功获取数据");
                }
            }
        });
    }
}
